package com.jmars.TS.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jmars.TS.common.JiumiConfig;
import com.jmars.TS.entity.SysJob;
import com.jmars.TS.mapper.db1.WMSInOutMapper;
import com.jmars.TS.service.NRIBService;
import com.jmars.TS.service.SysJobLogService;
import com.jmars.TS.service.SysJobService;
import com.jmars.TS.service.UserService;
import com.jmars.TS.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.CompletableFuture;

/**
 * @Author: cly
 * @DateTime: 2021/12/22 11:12
 * @msg:
 */

@CrossOrigin
@Controller
@ResponseBody
public class ApiController {
    private static final Logger logger = LoggerFactory.getLogger(ApiController.class);

    /** 系统基础配置 */
    @Autowired
    private JiumiConfig jiumiConfig;

    @Autowired
    NRIBService nribService;

    @Autowired
    UserService userService;

    @Autowired
    SysJobService sysJobService;

    @Autowired
    SysJobLogService sysJobLogService;

    //http://127.0.0.1:8057/api/getStandardVersion
    @ResponseBody
    @RequestMapping(value = "/api/getStandardVersion")
    public String getStandardVersion(){
        return "1.0";
    }

    //http://127.0.0.1:8057/api/getVendorVersion
    @ResponseBody
    @RequestMapping(value = "/api/getVendorVersion")
    public String getVendorVersion(){

        return "1.0.1.20241022";
    }



    @Autowired
    WMSInOutMapper wmsInOutMapper;

    // 1、商品主档
    //http://127.0.0.1:8057/api/inSkuTask
    @ResponseBody
        @RequestMapping(value = "/api/inSkuTask")
    public boolean inSkuTask(){

        // 商品主档
        boolean isSuccess = nribService.inSkuTask();
        return isSuccess;
    }

    // 2、店铺主档
    //http://127.0.0.1:8057/api/inLocationTask
    @ResponseBody
    @RequestMapping(value = "/api/inLocationTask")
    public boolean inLocationTask(){

        // 店铺主档
        boolean isSuccess = nribService.inLocationTask();
        return isSuccess;
    }

    // 3、供应商主档
    //http://127.0.0.1:8057/api/inSupplyTask
    @ResponseBody
    @RequestMapping(value = "/api/inSupplyTask")
    public boolean inSupplyTask(){

        // 供应商主档
        boolean isSuccess = nribService.inSupplyTask();

        return isSuccess;
    }

    // 4、路线数据
    //http://127.0.0.1:8057/api/inLineTask
    @ResponseBody
    @RequestMapping(value = "/api/inLineTask")
    public boolean inLineTask(){

        // 路线数据
        boolean isSuccess = nribService.inLineTask();

        return isSuccess;
    }

    // 5、收货指令接收
    //http://127.0.0.1:8057/api/inAsnTask
    //http://127.0.0.1:8057/api/inAsnTask?orderDay=20241020
    //http://127.0.0.1:8057/api/inAsnTask?startDate=20241118&endDate=20241118
    @ResponseBody
    @RequestMapping(value = "/api/inAsnTask")
    public boolean inAsnTask(String startDate,String endDate){


//        String startDate = DateUtils.getYMD();
//        String endDate = DateUtils.getYMD();

        if(startDate == null){
            startDate = DateUtils.getYMD();
        }
        if(endDate == null){
            endDate = DateUtils.getYMD();
        }


        // 收货指令接收
        boolean isSuccess = nribService.inAsnTask("01",startDate , endDate);
        boolean isSuccess2 = nribService.inAsnTask("02",startDate , endDate);


        return isSuccess;
    }

    // 6、发货指令接收
    //http://127.0.0.1:8057/api/inSoTask
    //http://127.0.0.1:8057/api/inSoTask?orderDay=20241020
    //http://127.0.0.1:8057/api/inSoTask?startDate=20250701&endDate=20250701
    //http://127.0.0.1:8057/api/inSoTask?soNo=Y2412040576000001
    @ResponseBody
    @RequestMapping(value = "/api/inSoTask")
    public boolean inSoTask(String startDate,String endDate,String soNo){


//        String startDate = DateUtils.getYMD();
//        String endDate = DateUtils.getYMD();

        if(startDate == null){
            startDate = DateUtils.getYMD();
        }
        if(endDate == null){
            endDate = DateUtils.getYMD();
        }

        if(soNo != null){
            startDate = null;
            endDate = null;
        }


        // 发货指令接收
        boolean isSuccess = nribService.inSoTask("01",startDate , endDate ,soNo);
        boolean isSuccess2 = nribService.inSoTask("02",startDate , endDate,soNo);

        return isSuccess2;
    }

    // 7、收货单回传
    //http://127.0.0.1:8057/api/outAsnTask
    @ResponseBody
    @RequestMapping(value = "/api/outAsnTask")
    public boolean outAsnTask(){

        // 收货单回传
        boolean isSuccess = nribService.outAsnTask();
        return isSuccess;
    }

    // 8、发货单回传
    //http://127.0.0.1:8057/api/outSoTask
    @ResponseBody
    @RequestMapping(value = "/api/outSoTask")
    public boolean outSoTask(){


        // 发货单回传
        boolean isSuccess = nribService.outSoTask();
        return isSuccess;
    }

    // 4.22 门店退仓库实际退货回传
    //http://127.0.0.1:8057/api/outAsn2Task
    @ResponseBody
    @RequestMapping(value = "/api/outAsn2Task")
    public boolean outAsn2Task(){

        // 收货单回传
        boolean isSuccess = nribService.outAsn2Task();
        return isSuccess;
    }

    // 11、退货单回传 (wms回传仓库退供应商接口)
    //http://127.0.0.1:8057/api/outSo2Task
    @ResponseBody
    @RequestMapping(value = "/api/outSo2Task")
    public boolean outSo2Task(){

        boolean isSuccess = nribService.outSo2Task();
        return isSuccess;
    }

    // 9、每日库存回传
    //http://127.0.0.1:8057/api/outInvTask
    @ResponseBody
    @RequestMapping(value = "/api/outInvTask")
    public boolean outInvTask(){

        boolean isSuccess = nribService.outInvTask();
        return isSuccess;
    }

    // 10、库存异动回传
    //http://127.0.0.1:8057/api/outInv2Task
    @ResponseBody
    @RequestMapping(value = "/api/outInv2Task")
    public boolean outInv2Task(){

        boolean isSuccess = nribService.outInv2Task();
        return isSuccess;
    }

    // 报损调整数据回传
    //http://127.0.0.1:8057/api/outInv2BSTask
    @ResponseBody
    @RequestMapping(value = "/api/outInv2BSTask")
    public boolean outInv2BSTask(){

        boolean isSuccess = nribService.outInv2BSTask();
        return isSuccess;
    }



    // 4.12 拣货单状态获取（旧的不用）
    //http://127.0.0.1:8057/api/getBilPickInfoTask
    @ResponseBody
    @RequestMapping(value = "/api/getBilPickInfoTask")
    public boolean getBilPickInfoTask(){

        boolean isSuccess = nribService.getBilPickInfoTask();
        return isSuccess;
    }

//    // 4.14 拣货单状态获取（新）(已弃用-使用新的list方式)
//    //http://127.0.0.1:8057/api/inOrderPickStsTask
//    @ResponseBody
//    @RequestMapping(value = "/api/inOrderPickStsTask")
//    public boolean getOrderPickStsTask(){
//
//        boolean isSuccess = nribService.inOrderPickStsTask();
//        return isSuccess;
//    }
//
//    // 4.25订单揽货状态获取List（dps切换后不用）
//    //http://127.0.0.1:8057/api/inOrderPickStsListTask
//    @ResponseBody
//    @RequestMapping(value = "/api/inOrderPickStsListTask")
//    public boolean getOrderPickStsListTask(){
//
//        boolean isSuccess = nribService.inOrderPickStsListTask();
//        return isSuccess;
//    }

    // 4.15 获取商品库存
    //http://127.0.0.1:8057/api/inWmsStockTask
    @ResponseBody
    @RequestMapping(value = "/api/inWmsStockTask")
    public boolean getWmsStockTask(){

        // 获取商品库存
        boolean isSuccess = nribService.inWmsStockTask();
        return isSuccess;
    }

    // 4.16 wms获取商品附加属性
    //http://127.0.0.1:8057/api/inInfGoodsMoreTask
    @ResponseBody
    @RequestMapping(value = "/api/inInfGoodsMoreTask")
    public boolean getInfGoodsMore(){

        // wms获取商品附加属性
        boolean isSuccess = nribService.inInfGoodsMoreTask();
        return isSuccess;
    }

    // 4.17 wms获取货位信息
    //http://127.0.0.1:8057/api/inInfStockPositionTask
    @ResponseBody
    @RequestMapping(value = "/api/inInfStockPositionTask")
    public boolean inInfStockPosition(){

        // wms获取商品附加属性
        boolean isSuccess = nribService.inInfStockPositionTask();
        return isSuccess;
    }

    // 4.20 门店退仓库退货申请
    //http://127.0.0.1:8057/api/getStoreToDcApply
    //http://127.0.0.1:8057/api/getStoreToDcApply?startDate=20241020&endDate=20241103
    @ResponseBody
    @RequestMapping(value = "/api/getStoreToDcApply")
    public boolean getStoreToDcApply(String startDate,String endDate){

//        String startDate = DateUtils.getYMD();
//        String endDate = DateUtils.getYMD();

        if(startDate == null){
            startDate = DateUtils.getYMD3();
        }
        if(endDate == null){
            endDate = DateUtils.getYMD();
        }

//        startDate = "20241025";
//        endDate = "20241103";

        boolean isSuccess = nribService.in_getStoreToDcApply(startDate,endDate);
        return isSuccess;
    }

    // 4.21 仓库退供应商退货申请
    //http://127.0.0.1:8057/api/getDcToSupplierApply
    //http://127.0.0.1:8057/api/getDcToSupplierApply?startDate=20241201&endDate=20241203
    @ResponseBody
    @RequestMapping(value = "/api/getDcToSupplierApply")
    public boolean getDcToSupplierApply(String startDate,String endDate){

//        String startDate = DateUtils.getYMD();
//        String endDate = DateUtils.getYMD();

        if(startDate == null){
            startDate = DateUtils.getYMD();
        }
        if(endDate == null){
            endDate = DateUtils.getYMD();
        }

//        startDate = "20241201";
//        endDate = "20241203";

        boolean isSuccess = nribService.in_getDcToSupplierApply(startDate,endDate);
        return isSuccess;
    }

    // 4.23 查询库存货位(拣货位)
    //http://127.0.0.1:8057/api/getWmsStockByPosition
    @ResponseBody
    @RequestMapping(value = "/api/getWmsStockByPosition")
    public boolean getWmsStockByPosition(){

        boolean isSuccess = nribService.in_getWmsStockByPosition();
        return isSuccess;
    }

    // 4.24获取pick详情
    //http://127.0.0.1:8057/api/inPickPrintLableInfoTask
    @ResponseBody
    @RequestMapping(value = "/api/inPickPrintLableInfoTask")
    public boolean inPickPrintLableInfoTask(){

        boolean isSuccess = nribService.inPickPrintLableInfoTask();
        return isSuccess;
    }

    // 4.26 保存拣货记录
    //http://127.0.0.1:8057/api/outPickerRecordTask
    @ResponseBody
    @RequestMapping(value = "/api/outPickerRecordTask")
    public boolean outPickerRecordTask(){

        boolean isSuccess = nribService.outPickerRecordTask();
        return isSuccess;
    }


    // 4.27 库区拣货监控
    //http://127.0.0.1:8057/api/getDeptPickStatus
    @ResponseBody
    @RequestMapping(value = "/api/getDeptPickStatus")
    public boolean getDeptPickStatus(){

        boolean isSuccess = nribService.getDeptPickStatus();
        return isSuccess;
    }

    // 4.2 门店拣货监控
    //http://127.0.0.1:8057/api/getStorePickStatus
    @ResponseBody
    @RequestMapping(value = "/api/getStorePickStatus")
    public boolean getStorePickStatus(){

        boolean isSuccess = nribService.getStorePickStatus();
        return isSuccess;
    }

    // 4.29 路线拣货监控
    //http://127.0.0.1:8057/api/getLinePickStatus
    @ResponseBody
    @RequestMapping(value = "/api/getLinePickStatus")
    public boolean getLinePickStatus(){

        boolean isSuccess = nribService.getLinePickStatus();
        return isSuccess;
    }

    // 4.30 检查超期二分之一通知信息
    //http://127.0.0.1:8057/api/checkOverdue
    //http://127.0.0.1:8057/api/checkOverdue?orderId=D2024113000006016&articleId=0113600
    //http://127.0.0.1:8057/api/checkOverdue?orderId=PR-3318012-998&articleId=0112742
    @ResponseBody
    @RequestMapping(value = "/api/checkOverdue")
    public String checkOverdue(String orderId,String articleId){

        String strResponseData = nribService.checkOverdue(orderId,articleId);
        return strResponseData;
    }

    // 4.31 读取退货商品数据(废弃不用)
//    //http://127.0.0.1:8057/api/getStoreReturnList
//    //http://127.0.0.1:8057/api/getStoreReturnList?vendorId=000237
//    @ResponseBody
//    @RequestMapping(value = "/api/getStoreReturnList")
//    public String getStoreReturnList(String vendorId){
//
//        String strResponseData = nribService.getStoreReturnList(vendorId);
//        return strResponseData;
//    }


    /** 4.35 保存拣货数量 **/
    //http://127.0.0.1:8057/api/outWmsGoodsTask
    @ResponseBody
    @RequestMapping(value = "/api/outWmsGoodsTask")
    public boolean outWmsGoodsTask(){

        boolean isSuccess = nribService.outWmsGoodsTask();
        return isSuccess;
    }

    /** 4.36 保存配送单 **/
    //http://127.0.0.1:8057/api/outDistributionOrderTask
    @ResponseBody
    @RequestMapping(value = "/api/outDistributionOrderTask")
    public boolean outDistributionOrderTask(){

        boolean isSuccess = nribService.outDistributionOrderTask();
        return isSuccess;
    }

    /** 4.37 获取大库成本 **/
    //http://127.0.0.1:8057/api/inDCCostTask
    @ResponseBody
    @RequestMapping(value = "/api/inDCCostTask")
    public boolean inDCCost(){

        boolean isSuccess = nribService.inDCCostTask();
        return isSuccess;
    }

    /** 4.38 获取供应商商品关系 **/
    //http://127.0.0.1:8057/api/inMasterVendorTask
    @ResponseBody
    @RequestMapping(value = "/api/inMasterVendorTask")
    public boolean inMasterVendorTask(){

        boolean isSuccess = nribService.inMasterVendorTask();
        return isSuccess;
    }




    // -----------------------
    // -- 定时任务
    // ----------------------

    private final Object lock_masterTask                    = new Object(); // 任务1的锁
    private final Object lock_scheduleOutInvTask            = new Object(); // 任务2的锁
    private final Object lock_scheduleInAsnTask             = new Object(); // 任务3的锁
    private final Object lock_scheduleInSoTask             = new Object(); // 任务3的锁
    private final Object lock_scheduleInToDCTask            = new Object(); // 任务4的锁
    private final Object lock_scheduleInAsnMissTask         = new Object(); // 任务5的锁
    private final Object lock_scheduleOutAsnTask            = new Object(); // 任务6的锁
    private final Object lock_scheduleOutPickerRecordTask            = new Object(); // 任务6的锁
    private final Object lock_scheduleInPickPrintLableInfoTask = new Object(); // 任务7的锁
    private final Object lock_scheduleOutWmsGoodsTask = new Object(); // 任务7的锁
    private final Object lock_scheduleOutDistributionOrderTask = new Object(); // 任务7的锁
    private final Object lock_scheduleInOrderPickStsTask    = new Object(); // 任务8的锁
    private final Object lock_scheduleHYStockATask          = new Object(); // 任务9的锁


    // 定时任务 - 主档数据导入数据库
    @RequestMapping("masterTask")
    @Scheduled(cron = "${scheduling.cron1}") //每天晚上21点执行一次    21点
    public void masterTask(){

        synchronized (lock_masterTask) { // 确保任务内部同步执行

            // 判断定时任务状态 （0-暂停 1-正常）
            if(!CheckJobState("masterTask")) { return;}

            logger.info("=== 档案数据每日同步 start ===");

            nribService.inInfGoodsMoreTask();       // wms获取商品附加属性
            nribService.inSkuTask();                // 商品档案（先调用附加信息再商品主档）
            nribService.inLocationTask();           // 门店档案
            nribService.inSupplyTask();             // 供应商档案
            nribService.inLineTask();               // 路线数据档案
            nribService.inWmsStockTask();           // 获取商品库存
            nribService.inInfStockPositionTask();   // 货位档案（wms获取货位信息）
            nribService.in_getWmsStockByPosition();   // 查询库存货位（拣货位）


            logger.info("=== 档案数据每日同步 end   ===");
        }

    }

    @RequestMapping("scheduleOutInvTask")
    @Scheduled(cron = "${scheduling.cronOutInv}") //每天定时间隔时间执行
    public void scheduleOutInvTask(){

        synchronized (lock_scheduleOutInvTask) { // 确保任务内部同步执行

            // 判断定时任务状态 （0-暂停 1-正常）
            if(!CheckJobState("scheduleOutInvTask")) { return;}

            logger.info("=== 每日库存数据回传 start ===");

            nribService.outInvTask();           // 每日库存回传（不用）
            nribService.outInv2Task();          // 盘点调整数据回传
            nribService.outInv2BSTask();        // 报损调整数据回传

            logger.info("=== 每日库存数据回传 end   ===");
        }


    }

    @RequestMapping("scheduleInAsnTask")
    @Scheduled(cron = "${scheduling.cronInAsn}") //每天定时间隔时间执行
    public void scheduleInAsnTask(){

        synchronized (lock_scheduleInAsnTask) { // 确保任务内部同步执行
            // 判断定时任务状态 （0-暂停 1-正常）
            if(!CheckJobState("scheduleInAsnTask")) { return;}

            logger.info("=== 订单数据asn定时同步 start ===");

            // 定时间隔回传
            String startDate = DateUtils.getYMD3();  // 查询条件 3天内
            String endDate = DateUtils.getYMD();


            CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> nribService.inAsnTask("01", startDate, endDate));

            CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> nribService.inAsnTask("02", startDate, endDate));

            CompletableFuture.allOf(future1,  future3).join();


            logger.info("=== 订单数据asn定时同步 end   ===");
        }

    }

    @RequestMapping("scheduleInSoTask")
    @Scheduled(cron = "${scheduling.cronInSo}") //每天定时间隔时间执行
    public void scheduleInSoTask(){

        synchronized (lock_scheduleInSoTask) { // 确保任务内部同步执行
            // 判断定时任务状态 （0-暂停 1-正常）
            if(!CheckJobState("scheduleInSoTask")) { return;}

            logger.info("=== 订单数据so定时同步 start ===");

            // 定时间隔回传
            String startDate = DateUtils.getYMD3();  // 查询条件 3天内
            String endDate = DateUtils.getYMD();



            CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> nribService.inSoTask("01", startDate, endDate, null));
            CompletableFuture<Void> future4 = CompletableFuture.runAsync(() -> nribService.inSoTask("02", startDate, endDate, null));

            CompletableFuture.allOf( future2,  future4).join();


            logger.info("=== 订单数据so定时同步 end   ===");
        }

    }

    @RequestMapping("scheduleInToDCTask")
    @Scheduled(cron = "${scheduling.cronInToDC}") //每天定时间隔时间执行
    public void scheduleInToDCTask(){

        synchronized (lock_scheduleInToDCTask) { // 确保任务内部同步执行

            // 判断定时任务状态 （0-暂停 1-正常）
            if(!CheckJobState("scheduleInToDCTask")) { return;}

            logger.info("=== 退货数据定时同步 start ===");


            // 定时间隔回传
            String startDate = DateUtils.getYMD3(); // 查询条件 3天内
            String endDate = DateUtils.getYMD();

            CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> nribService.in_getStoreToDcApply(startDate, endDate));
            CompletableFuture<Void> future4 = CompletableFuture.runAsync(() -> nribService.in_getDcToSupplierApply(startDate, endDate));

            CompletableFuture.allOf(future3, future4).join();


            logger.info("=== 退货数据定时同步 end   ===");
        }

    }

    //http://127.0.0.1:8057/scheduleInAsnMissTask
    @RequestMapping("scheduleInAsnMissTask")
    @Scheduled(cron = "0 0 8,12,15,18 * * ?") //每天定点8,12,15,18点同步一下这四种订单
    public void scheduleInAsnMissTask(){

        synchronized (lock_scheduleInAsnMissTask) { // 确保任务内部同步执行

            // 判断定时任务状态 （0-暂停 1-正常）
            if(!CheckJobState("scheduleInAsnMissTask")) { return;}

            logger.info("=== 每天定时8,12,15点过去订单数据定时同步 start ===");


            // 定时间隔回传
            String startDate = DateUtils.getAmountDayYMD(-9);
            String endDate = DateUtils.getAmountDayYMD(-3);


            CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> nribService.inAsnTask("01", startDate, endDate));
            CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> nribService.inSoTask("01", startDate, endDate, null));
            CompletableFuture<Void> future5 = CompletableFuture.runAsync(() -> nribService.inAsnTask("02", startDate, endDate));
            CompletableFuture<Void> future6 = CompletableFuture.runAsync(() -> nribService.inSoTask("02", startDate, endDate, null));

            CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> nribService.in_getStoreToDcApply(startDate, endDate));
            CompletableFuture<Void> future4 = CompletableFuture.runAsync(() -> nribService.in_getDcToSupplierApply(startDate, endDate));


            CompletableFuture.allOf(future1, future2, future3, future4 , future5, future6).join();


            logger.info("=== 每天定时8,12,15点过去订单数据定时同步 end   ===");
        }

    }

    @RequestMapping("scheduleOutAsnTask")
    @Scheduled(cron = "${scheduling.cronOutAsn}") //每天定时间隔时间执行
    public void scheduleOutAsnTask(){

        synchronized (lock_scheduleOutAsnTask) { // 确保任务内部同步执行
            // 判断定时任务状态 （0-暂停 1-正常）
            if(!CheckJobState("scheduleOutAsnTask")) { return;}

            logger.info("=== 回传数据定时同步 start ===");


            CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> nribService.outAsnTask());
            CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> nribService.outAsn2Task());
            CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> nribService.outSoTask());
            CompletableFuture<Void> future4 = CompletableFuture.runAsync(() -> nribService.outSo2Task());
            //CompletableFuture<Void> future5 = CompletableFuture.runAsync(() -> nribService.outPickerRecordTask());

            CompletableFuture.allOf(future1, future2, future3, future4).join();

            logger.info("=== 回传数据定时同步 end   ===");
        }

    }

    @RequestMapping("scheduleOutPickerRecordTask")
    @Scheduled(cron = "${scheduling.cronOutPickerRecord}") //每天定时间隔时间执行
    public void scheduleOutPickerRecordTask(){

        synchronized (lock_scheduleOutPickerRecordTask) { // 确保任务内部同步执行
            // 判断定时任务状态 （0-暂停 1-正常）
            if(!CheckJobState("scheduleOutPickerRecordTask")) { return;}

            logger.info("=== 回传拣货绩效数据定时同步 start ===");


            CompletableFuture<Void> future5 = CompletableFuture.runAsync(() -> nribService.outPickerRecordTask());

            CompletableFuture.allOf(future5).join();

            logger.info("=== 回传拣货绩效数据定时同步 end   ===");
        }

    }


    // 回传
    @RequestMapping("scheduleOutWmsGoodsTask")
    @Scheduled(cron = "${scheduling.cronOutWmsGoods}") //每天定时间隔时间执行
    public void scheduleOutWmsGoodsTask(){
        synchronized (lock_scheduleOutWmsGoodsTask) { // 确保任务内部同步执行

            // 判断定时任务状态 （0-暂停 1-正常）
            if(!CheckJobState("scheduleOutWmsGoodsTask")) { return;}

            logger.info("=== 回传拣货单数量 start ===");

            boolean isSuccess2 = nribService.outWmsGoodsTask();

            logger.info("=== 回传拣货单数量 end   ===");
        }

    }

    // 回传
    @RequestMapping("scheduleOutDistributionOrderTask")
    @Scheduled(cron = "${scheduling.cronOutDistributionOrder}") //每天定时间隔时间执行
    public void scheduleOutDistributionOrderTask(){
        synchronized (lock_scheduleOutDistributionOrderTask) { // 确保任务内部同步执行

            // 判断定时任务状态 （0-暂停 1-正常）
            if(!CheckJobState("scheduleOutDistributionOrderTask")) { return;}

            logger.info("=== 回传配送单信息 start ===");

            boolean isSuccess2 = nribService.outDistributionOrderTask();

            logger.info("=== 回传配送单信息 end   ===");
        }

    }


    // DPS切换后不再使用
//    @RequestMapping("scheduleInPickPrintLableInfoTask")
//    @Scheduled(cron = "${scheduling.cronInPickPrintLableInfo}") //每天定时间隔时间执行
//    public void scheduleInPickPrintLableInfoTask(){
//        synchronized (lock_scheduleInPickPrintLableInfoTask) { // 确保任务内部同步执行
//
//            // 判断定时任务状态 （0-暂停 1-正常）
//            if(!CheckJobState("scheduleInPickPrintLableInfoTask")) { return;}
//
//            logger.info("=== 获取标签打印数据、拣货单状态 start ===");
//
//            boolean isSuccess2 = nribService.inPickPrintLableInfoTask();    // 获取标签打印数据     更新状态为2
//            boolean isSuccess3 = nribService.inOrderPickStsListTask();      // 拣货单状态获取List   更新状态为3
//
//
//            logger.info("=== 获取标签打印数据、拣货单状态 end   ===");
//        }
//
//    }

    @RequestMapping("scheduleHYStockATask")
    @Scheduled(cron = "0 15 6 * * ?") //每天早上6:15分同步下库存
    public void scheduleHYStockATask(){

        synchronized (lock_scheduleHYStockATask) { // 确保任务内部同步执行

            // 判断定时任务状态 （0-暂停 1-正常）
            if(!CheckJobState("scheduleHYStockATask")) { return;}

            logger.info("=== 同步HY库存 start ===");

            boolean isSuccess14 = nribService.syncHYStockATask();   // 备份表
            boolean isSuccess1 = nribService.inDCCostTask();        // 获取大库成本
            boolean isSuccess2 = nribService.inMasterVendorTask();  // 同步供应商商品

            logger.info("=== 同步HY库存 end   ===");
        }

    }


//    private final Object lock1 = new Object(); // 任务1的锁
//    private final Object lock2 = new Object(); // 任务2的锁
//
//    @Scheduled(fixedRate = 5000) // 每5秒执行一次
//    public void TestJob() {
//        synchronized (lock1) { // 确保任务内部同步执行
//
//            String jobName = "TestJob";
//            if(!CheckJobState(jobName)) { return;}
//
//            logger.info("定时任务{} 执行成功 on thread: {}",jobName,Thread.currentThread().getName());
//        }
//    }
//
//    @Scheduled(fixedRate = 3000) // 每3秒执行一次
//    public void Test2Job() {
//        synchronized (lock2) { // 确保任务内部同步执行
//
//            String jobName = "Test2Job";
//            if(!CheckJobState(jobName)) { return;}
//
//            logger.info("定时任务{} 执行成功 on thread: {}",jobName,Thread.currentThread().getName());
//
//        }
//    }


    // 查询定时任务状态（0-暂停 1-正常）
    private boolean CheckJobState(String jobName) {

        SysJob sysJob = sysJobService.getOne(new QueryWrapper<SysJob>().eq("job_name", jobName));
        if(sysJob == null ) { logger.info("定时任务{} 状态：不存在",jobName); return false; }

        if(sysJob.getStatus().equals("1")) {
//            logger.info("定时任务{} 状态执行成功 on thread: {}",jobName,Thread.currentThread().getName());
            logger.info("定时任务{} 状态：正常运行",jobName); return true;
        }else {
            logger.info("定时任务{} 状态：已暂停",jobName); return false;
        }

    }

}
